package models

import (
	"time"
	"github.com/astaxie/beego/cache"
	"fmt"
	_ "github.com/astaxie/beego/cache/redis"
	"github.com/garyburd/redigo/redis"
)

func PutIntoCache(key string, value interface{}, times time.Duration) error {
	bm, err := cache.NewCache("redis", `{"key":"common","conn":"127.0.0.1:6379","dbNum":"0"}`)
	if err != nil {
		fmt.Println(err)
		return err
	}
	if err = bm.Put(key, value, times); err != nil {
		return err
	}
	return nil
}

func GetCache(key string) (string, error) {
	bm, err := cache.NewCache("redis", `{"key":"common","conn":"127.0.0.1:6379","dbNum":"0"}`)
	if err != nil {
		fmt.Println(err)
		return "", err
	}
	v, err := redis.String(bm.Get(key), err)
	return v, err
}

func GetCacheInt(key string) (int, error) {
	bm, err := cache.NewCache("redis", `{"key":"common","conn":"127.0.0.1:6379","dbNum":"0"}`)
	if err != nil {
		fmt.Println(err)
		return 0, err
	}
	v, err := redis.Int(bm.Get(key), err)
	return v, err
}

func GetCacheBool(key string) (bool, error) {
	bm, err := cache.NewCache("redis", `{"key":"common","conn":"127.0.0.1:6379","dbNum":"0"}`)
	if err != nil {
		fmt.Println(err)
		return false, err
	}
	v, err := redis.Bool(bm.Get(key), err)
	return v, err
}

func DeleteCache(key string) {
	bm, _ := cache.NewCache("redis", `{"key":"common","conn":"127.0.0.1:6379","dbNum":"0"}`)
	bm.Delete(key)
}

func IncreaseCount(key string) error {
	bm, _ := cache.NewCache("redis", `{"key":"common","conn":"127.0.0.1:6379","dbNum":"0"}`)
	return bm.Incr(key)
}
